package m.app.action;

import com.alibaba.fastjson.JSONObject;
import m.base.model.AppInfo;
import m.base.model.StoreInfo;
import m.base.model.UserInfo;
import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.model.util.*;
import m.fun.model.CountInfo;
import m.fun.model.ProduceInfo;
import m.store.model.*;
import m.store.service.MonthRecordService;
import m.system.exception.MException;
import m.system.util.ActionUtil;
import m.system.util.DateUtil;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@ActionMeta(name = "app/index",title = "app首页统计")
public class IndexAction extends AppAction {
    public JSONMessage dataMap() throws Exception {
        UserInfo user = PowerUtil.getSessionUser(true);
        StoreInfo store = PowerUtil.getSessionStore();
        if (null == store || ObjectUtil.isEmptyOid(store.getDept()))
            throw new MException(this.getClass(), "无商户信息");
        Date date = new Date();
        List<MonthRecord> ls=getService(MonthRecordService.class).getList(user.getOid(),date);
        JSONObject data=new JSONObject();
        for(MonthRecord m : ls){
            switch (m.getType()){
                case MonthRecord.Type_Stock:
                    data.put("userMonthStockNum",m.getNum());
                    data.put("userMonthStockAmount",m.getAmount());
                    break;
                case MonthRecord.Type_Sale:
                    data.put("userMonthSaleNum",m.getNum());
                    data.put("userMonthSaleAmount",m.getAmount());
                    break;
                case MonthRecord.Type_Pay:
                    data.put("userMonthPayNum",m.getNum());
                    data.put("userMonthPayAmount",m.getAmount());
                    break;
                case MonthRecord.Type_Income:
                    data.put("userMonthIncomeNum",m.getNum());
                    data.put("userMonthIncomeAmount",m.getAmount());
                    break;
            }
        }
        //出入库审核
        if(PowerUtil.hasPower(PowerUtil.Stock)){
            data.put("stockPower",true);
            ActionUtil.fillJSONObject(data, ModelQuery.init(OrderRepositoryCheck.class)
                    .addCollectExp("userCheckRepositoryNum","ifnull(sum(1),0)")
                    .addCollectExp("userCheckRepositoryAmount","ifnull(sum(#{amount}),0)")
                    .addCondition(
                            QueryCondition.eq("checkUser.oid",user.getOid()),
                            QueryCondition.eq("status",OrderRepositoryCheck.Status_Check)
                    ).queryCollect().get(0)
            );
            ActionUtil.fillJSONObject(data,ModelQuery.init(StockInfo.class)
                    .addCollectExp("userCheckStockNum","ifnull(sum(1),0)")
                    .addCollectExp("userCheckStockAmount","ifnull(sum(#{stockAmount}),0)")
                    .addCondition(
                            QueryCondition.eq("store.oid",store.getOid()),
                            QueryCondition.eq("status", OrderModel.Status_Check)
                    ).queryCollect().get(0)
            );
            ActionUtil.fillJSONObject(data,ModelQuery.init(SaleInfo.class)
                    .addCollectExp("userCheckSaleNum","ifnull(sum(1),0)")
                    .addCollectExp("userCheckSaleAmount","ifnull(sum(#{saleAmount}),0)")
                    .addCondition(
                            QueryCondition.eq("store.oid",store.getOid()),
                            QueryCondition.eq("status", OrderModel.Status_Check)
                    ).queryCollect().get(0)
            );
            ActionUtil.fillJSONObject(data,ModelQuery.init(ProduceInfo.class)
                    .addCollectExp("userCheckProduceNum","ifnull(sum(1),0)")
                    .addCollectExp("userCheckProduceAmount","ifnull(sum(#{produceAmount}),0)")
                    .addCondition(
                            QueryCondition.eq("store.oid",store.getOid()),
                            QueryCondition.eq("status",OrderModel.Status_Check)
                    ).queryCollect().get(0)
            );
            ActionUtil.fillJSONObject(data,ModelQuery.init(CountInfo.class)
                    .addCollectExp("userCheckCountNum","ifnull(sum(1),0)")
                    .addCollectExp("userCheckCountAmount","ifnull(sum(#{countAmount}),0)")
                    .addCondition(
                            QueryCondition.eq("store.oid",store.getOid()),
                            QueryCondition.eq("status",OrderModel.Status_Check)
                    ).queryCollect().get(0)
            );
        }
        //收付款审核
        if(PowerUtil.hasPower(PowerUtil.Finance)){
            data.put("financePower",true);
            ActionUtil.fillJSONObject(data,ModelQuery.init(PayAmount.class)
                    .addCollectExp("userCheckPayNum","ifnull(sum(1),0)")
                    .addCollectExp("userCheckPayAmount","ifnull(sum(#{payAmount}),0)")
                    .addCondition(
                            QueryCondition.eq("store.oid",store.getOid()),
                            QueryCondition.eq("status",OrderModel.Status_Check)
                    ).queryCollect().get(0)
            );
            ActionUtil.fillJSONObject(data,ModelQuery.init(IncomeAmount.class)
                    .addCollectExp("userCheckIncomeNum","ifnull(sum(1),0)")
                    .addCollectExp("userCheckIncomeAmount","ifnull(sum(#{incomeAmount}),0)")
                    .addCondition(
                            QueryCondition.eq("store.oid",store.getOid()),
                            QueryCondition.eq("status",OrderModel.Status_Check)
                    ).queryCollect().get(0)
            );
        }
        //当月销售排行
        List<MonthRecord> mls= ModelQueryList.getModelList(MonthRecord.class,new String[]{"*","user.realname"},
                new QueryPage(0,10),
                QueryCondition.and(
                        QueryCondition.eq("store.oid",store.getOid()),
                        QueryCondition.eq("yearMonth",DateUtil.format(date,MonthRecord.YearMonth_Format)),
                        QueryCondition.eq("type",MonthRecord.Type_Sale)
                ),
                QueryOrder.desc("amount")
        );
        List<JSONMessage> monthList=new ArrayList<>();
        for(MonthRecord m : mls){
            monthList.add(
                    new JSONMessage()
                            .push("name",m.getUser().getRealname())
                            .push("amount",m.getAmount())
                            .push("num",m.getNum())
            );
        }
        data.put("monthTitle",DateUtil.format(date,"yyyy年MM月"));
        data.put("monthList",monthList);
        return new JSONMessage(0)
            .push("dataMap", data)
            .push("date", DateUtil.format(date, "yyyy年MM月dd日 EEEE"));
    }
}
